package com.web.cms.pan.provider;

import com.blue.jdbc.Page;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.web.cms.pan.exception.ShareException;
import com.web.cms.pan.facade.ShareFacade;
import com.web.cms.pan.message.ShareLogRequest;
import com.web.cms.pan.message.ShareLogResponse;
import com.web.cms.pan.message.ShareRequest;
import com.web.cms.pan.message.ShareResponse;
import com.web.cms.pan.model.Share;
import com.web.cms.pan.model.ShareLog;
import com.web.cms.pan.service.ShareService;
import com.web.cms.pan.util.ShareConvertor;
import com.web.cms.pan.util.ShareLogConvertor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 共享接口实现类
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2018年04月09日
 */
@Component("panShareProvider")
public class ShareProvider implements ShareFacade
{
	@Autowired
	private ShareService shareService;
	
	public ShareProvider()
	{
	}

	@Override
	public ShareResponse add(ShareRequest request)
	{
		ValidationUtil.valid(request, SaveModel.class);
		Share share = ShareConvertor.toShare(request);
		share = shareService.save(share);
		ShareResponse response = ShareConvertor.toResponse(share);
		return response;
	}

	@Override
	public ShareResponse get(Integer id)
	{
		if (id == null)
			throw new ShareException("Id不能为空");
			
		Share share = shareService.get(id);
		ShareResponse response = ShareConvertor.toResponse(share);
		return response;
	}

	@Override
	public ShareResponse getByCode(String code)
	{
		return null;
	}

	@Override
	public void delete(Integer id)
	{

	}

	@Override
	public Page<ShareResponse> listPage(ShareRequest request, int start, int size)
	{
		Share share = ShareConvertor.toShare(request);
		Page<Share> page = new Page<>(size);
		page.setCurPage(start);
		page = shareService.list(share, page);
		Page<ShareResponse> pageResponse = ShareConvertor.toResponse(page);
		return pageResponse;
	}

	@Override
	public Page<ShareLogResponse> listLogPage(ShareLogRequest request, int start, int size)
	{
		ShareLog shareLog = ShareLogConvertor.toShareLog(request);
		Page<ShareLog> page = new Page<>(size);
		page.setCurPage(start);
		page = shareService.listLog(shareLog, page);
		Page<ShareLogResponse> pageResponse = ShareLogConvertor.toResponse(page);
		return pageResponse;
	}

}
